import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddActivityLocationFields1625000000000 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    // 添加新字段
    await queryRunner.query(`
      ALTER TABLE activities 
      ADD COLUMN type VARCHAR(255) NOT NULL DEFAULT '休闲骑行',
      ADD COLUMN startLocation VARCHAR(255),
      ADD COLUMN startAddress VARCHAR(255),
      ADD COLUMN endLocation VARCHAR(255),
      ADD COLUMN endAddress VARCHAR(255);
    `);

    // 将现有的location和address数据复制到新字段
    await queryRunner.query(`
      UPDATE activities 
      SET startLocation = location, 
          startAddress = address,
          endLocation = location,
          endAddress = address
      WHERE location IS NOT NULL;
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      ALTER TABLE activities 
      DROP COLUMN type,
      DROP COLUMN startLocation,
      DROP COLUMN startAddress,
      DROP COLUMN endLocation,
      DROP COLUMN endAddress;
    `);
  }
}